/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


//import com.classnet.ClassNet;
import com.classnet.main.ClassNet;
import com.classnet.db.*;
import java.io.*;
import java.net.*;

import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Derek
 */

// TODO : handle multi-line description
public class CourseNewsGroupServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    String courseSubject = "CPSC";
    String courseCode= "410";
    String courseYear ="2007";
    String courseTerm ="Fall";
    String courseTitle="Advanced Software Engineering";
    String description ="Specification, design, construction and validation of multi-version software systems. ";
    
    
    String[] ngroupSections = {"Homework","Exams","General"};
    String[] ngroupTopicsUnderGeneral = 
    {"I wonder how many teams are doing this facebook thing??",
     "It might take me a while to get this threading thing to work..",
     "Vancouver's weather is really pissing me off....",
     "This example demonstrates how to display elements inside other elements.",
     "This example demonstrates a table with no borders.",
     "This example demonstrates how to display table headers.",
     "This example demonstrates how to add a background to one or more table cells"
    };
    String messageBody = "Set the background color"+
"Set an image as the background"+
"How to repeat a background image"+
"How to repeat a background image only vertically"+
"How to repeat a background image only horizontally"+
"How to display a background image only one time"+
"How to place the background image"+
"How to position a background image using %"+
"How to position a background image using pixels"+
"A fixed background image (this image will not scroll with the rest of the page)"+
"All the background properties in one declaration ";
    
    String poster ="Jinlin Chen";
    String posterEmail ="chenjinlin@hotmail.com";
    String postDateTime ="18:23:30 3rd Nov 2007";

    
  //  private ClassNet classnet = ClassNet.INSTANCE;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        //request.
        HttpSession httpSession = request.getSession(true);
        httpSession.setAttribute("usersession", ClassNet.INSTANCE.login("chenjinlin@hotmail.com", "password"));
        
        PrintWriter out = response.getWriter();
        try {
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CourseNewsGroupServlet</title>");  
           
            generateInlineJS(out);
            CSSGenerator.generateNewsGroupMainPageInlineCSS(out);
            
            out.println("</head>");
            out.println("<body>");
            //out.println("<h1>Servlet CourseNewsGroupServlet at " + request.getContextPath () + "</h1>");
            //out.println("<h1>Servlet get parameters at " + request.getParameter("testing") + "</h1>");
            out.println(
                    "<center>" +
                    "<table width=\"60%\" border>" +// style=\"border-color:#336699 \" border=\""+ngroupOuterTableBorderWidth+"\">" +
                    "<tr>" +
                    "   <td>" +
                    "       <p class=\"ngroupTitle\"> " +
                    "       Course" +     // title
                    "       </p>"  +
                    "   </td>" +
                    "   <td>" +
                    "        <p> " + courseSubject + " " + courseCode + "</p>" +
                    "   </td>" +
                    "   <td>" + 
                    "       <p class=\"ngroupTitle\"> " +
                    "       Year" +       // year
                    "       </p>" +
                    "   </td>" +
                    "   <td>" +
                    "        <p> "  + courseYear + " " + courseTerm + "</p>" +
                    "   </td>" +
                    "</tr>"+
                    "<tr>" +
                    "   <td>" +
                    "       <p class=\"ngroupTitle\"> " +
                    "       Title " + // coruse Title
                    "       </p>" + 
                    "   </td>"  +
                    "   <td>" + 
                    "<a href=\"https://courses.students.ubc.ca/cs/main?pname=subjarea&tname=subjareas&req=3&dept="+courseSubject+"&course="+courseCode+"\">" +
                    courseTitle + 
                    "</a>" +
                    "   </td>" +
                    "</tr>" +
                    "<tr>" +
                    "   <td>" +
                    "       <p class=\"ngroupTitle\"> " +
                    "       Description " + // description
                    "       </p>" + 
                    "   </td>"  +
                    "   <td>" + description +
                    "   </td>" +
                    "</tr>" +
                // end of course news group table info section 
                    
                    "<tr>" +
                    "    <td> " +
                    "       <p class=\"ngroupTitle\">" +
                    "           News Group" +
                    "       </p>" +
                    "    </td>" +
                    "    <td>" +
                    "    </td>" +
                    "    <td colspan=\"2\">" +
                    "    <button onclick=\"" +
                    //"window.open(\'CourseNewsGroupServlet?newPost=true\', '_blank', 'width=500,height=400,status=1,menubar=0,resizable=0 '); return false;\">" +
                    "window.open(\'NewPostServlet?courseSubject=" + courseSubject + "&courseCode=" + courseCode +
                    "\', '_blank', 'width=500,height=400,status=1,menubar=0,resizable=0 '); return false;\">" +
                    "New Post</button>" +
                    "    </td>" +
                    "</tr>");
            
                               
                    
                    
            for(int i =0;i < ngroupSections.length; i++)
            {  
                  out.println(
                     "<tr>" +     
                     "  <td>" +
                     "  </td>" +
                     "  <td colspan=\"3\">" +
                     "  <p class=\"ngroupSectionLinkText\">" +
                     "  <a href=\"CourseNewsGroupServlet?sections=" + ngroupSections[i] +"\">"
                     );
                  out.println(ngroupSections[i]);
                  out.println(
                     " </a>" +
                     " </p>" +
                     "  </td>" +        
                     "</tr>"
                     );  
                  
                  if( (request.getParameter("sections") != null) &&
                      (request.getParameter("sections").compareToIgnoreCase("General") ==0 )&&
                      (ngroupSections[i].compareToIgnoreCase("General") ==0))
                  {
                     
                      for(int j =0;j < ngroupTopicsUnderGeneral.length; j++)
                      { 
                            out.println(  //ngroupTopicsUnderGeneral
                                "<tr>" +     
                                "  <td>" +
                                "  </td>" +
                                "  <td colspan=\"3\"");
                            if(j % 2 ==0 )
                                out.println(" bgcolor=\"#ECEBEB\"");
                            out.println(
                                ">" +
                                " <li type =\"square\">" +
                                "  <p class=\"ngroupSectionLinkText\">" +
                               // "onClick="loadwindow(3)""
                                /*
                                "  <a href=\"\" onClick=\"loadwindow(\""+
                                "CourseNewsGroupServlet?sections="+ngroupSections[i] + "&" +
                                "topicIndex=" + j + "\"" +  ") >"
                               */
                                /*
                                "<a href=\"javascript:window.open(\'" +
                                //"LoginServlet" +
                                "CourseNewsGroupServlet?sections="+ngroupSections[i] + "&" +
                                "topicIndex=" + j +
                                "\',\'\',\'width=800,height=600,status=1,menubar=0,resizable=1\' )\" >"
                                */
                                "<a href=\""+
                                "CourseNewsGroupServlet?sections="+ngroupSections[i] + "&" +
                                "topicIndex=" + j + "\" " +
                                "target=\"_blank\" onclick=\"" +
                                "window.open(this.href, '_blank', 'width=500,height=400,status=1,menubar=0,resizable=0 '); return false;\">"
                                //"\')\" >" //target=\"popup\">"
                                /*
                                "  <a href=\"CourseNewsGroupServlet?sections=" +
                                   ngroupSections[i] + "&" +
                                   //ngroupTopicsUnderGeneral[j] +"\">"  // or a different servlet
                                   // or use the index
                                   "topicIndex=" + j + "\" " +
                                   "target=\"popup\">"
                                */  
                            );
                            out.println(ngroupTopicsUnderGeneral[j]);
                            out.println(
                             " </a>" +
                             " </p>" +
                             " </li>" +
                             "  </td>" +        
                             "</tr>"
                             );  
                      }
                     
                  }// displaying topics in sections  
                    
            } // displaying sections        
                    
            
            // end table tag
            out.println(        
                    "</table>" +
                    "<center>"
                    );
            
            
            
            
            // end html
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            out.close();
        }
    } 
   
     protected void handlePopupMessage(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         String topic;
         int topicIndex;
         
         topicIndex =Integer.parseInt(request.getParameter("topicIndex"));
         topic = ngroupTopicsUnderGeneral[topicIndex];
         
         response.setContentType("text/html;charset=UTF-8");
         PrintWriter out = response.getWriter();
         try {
             
            out.println("<html>");
            out.println("<head>");
            out.println("<title>News Group Post</title>"); 
            
             CSSGenerator.generateNewsGroupPopupReplayInlineCSS(out);
            
            out.println("</head>");
            out.println("<body>");
            
            
            
            out.println(
            //"<form>" +
            "<center>" +
            "<table height=\"380\" width=\"480\" border=\"0\">" +// style=\"border-color:#336699 \" border=\""+ngroupOuterTableBorderWidth+"\">" +
            "<tr height=\"10\">" +
            "   <td colspan=\"4\">" +
            "       <p class=\"ngroupMessageLabel\"> " +
            "       Message Topic" +     // title
            "       </p>"  +
            "   </td>" +
            "</tr>"+   
            "<tr height=\"10\">" +
            "   <td colspan=\"4\">" +
            "   <input type=\"text\" size=\"72\" name=messageTitle readonly value=\" " +
            topic+
            "   \">" +
            "   </td>" +
            "</tr>" +
            
            "<tr height=\"10\" >" +
            "   <td>" +
            "       <p class=\"ngroupMessageLabel\"> " +
            "       Message Body" +     // Message Body
            "       </p>"  +
            "   </td>" +
            "   <td colspan=\"3\" align =\"right\">" + 
            "       <p class =\"postInfo\">" +
            "       Posted by " + "<a href=\"UserServlet?user=" + posterEmail +"\" target=\"_blank\">"+  
                    poster + "</a>" +
            " at " +  postDateTime + ". " +
            "       </p>" +
            "   </td>"+
            "</tr>"+
                    
            // message body        
            "<tr height=\"40\">" +
            "   <td valign=\"top\" colspan=\"4\">" +
            //"   <input type=\"TEXTAREA\"  readonly " +
            "   <textarea rows=\"10\" cols=\"56\" readonly " +        
            "    name=messageBody >" +
            messageBody+
            "   </textarea>" +
            "   </td>" +
            "</tr>" +
             
            // reply
             "<tr height=\"10\" >" +
            "   <td colspan=\"4\">" +
            "       <p class=\"ngroupMessageLabel\"> " +
            "       Reply" +     // reply
            "       </p>"  +
            "   </td>" +
            "</tr>"+
            "<tr height=\"40\">" +
            "   <td valign=\"top\" colspan=\"4\">" +
            "<form name=\"replyForm\" method=\"POST\" action=\"TempServlet\">" +
            //"   <input type=\"TEXTAREA\"  readonly " +
            "   <textarea rows=\"6\" cols=\"56\" " +        
            "    name=messageReply >" +
            "</textarea>" +
            "   </td>" +
            "</tr>" +
                    
            "<tr height=\"7\" valign=\"top\">" +          //  |
            "   <td width=\"120\" valign=\"middle\">" +   //  | these two needs to be sorted out
            //"   &nbsp" +
            "   <p class=\"copyRight\"> ClassNet© 2007  </p>"  +
            "   </td>" +
            "   <td width=\"120\">&nbsp</td>" +
            "   <td width=\"200\" align =\"right\" valign=\"top\" >"+
           // "<form name=\"replyForm\" method=\"POST\" action=\"PostServlet\">" +
            " <input type=\"submit\" name=\"postReply\" value=\"Reply\" >" +//onClick=\"window.close()\">" +
            "</form>" +
          //  "   <button>Reply</button>"+  
            "   </td>" +
            "   <td width=\"40\" align =\"right\" valign=\"top\">"+
            "   <button onclick=\"window.close()\">Close</button>"+  
            "   </td>" +                    
            "</tr>" +// +
            /*
            "<tr height=\"7\">" +
            "    <td colspan=\"4\" align =\"right\">" +
            "    <p class=\"copyRight\"> ClassNet© 2007  </p>" + 
            "    </td>" +
            "</tr>" +*/
            "</table>"+
            "</center>"
            
            );
            
            
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            out.close();
        }
     }
     private void generateInlineJS(PrintWriter out)
     {
         out.println(
         "<script language=\"javascript\"> " +
         "function loadwindow(target)" +
         "{" +
         "       window.open(target,\"\",\"width=800,height=600,status=1,menubar=0,resizable=1\");" +
        /*
         "  switch(target) " +
         "  {"+
         "      case 1: window.open(\"/soc/Resume.pdf\",\"\",\"width=800,height=600,status=1,menubar=0,resizable=1\");" +
	 "       break;" +
	 "      default: break;" +
	 "  }" +
        */
         "}" +
         "</script>"
         );
    }

        
   
     
     
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        courseSubject = request.getParameter("courseSubject");
        courseCode = request.getParameter("courseCode");
        if(courseSubject == null)
            courseSubject = "CPSC";
        if(courseCode == null)
            courseCode= "410";
        
        //Course course = new Course();
        
        
        if(request.getParameter("topicIndex") == null) // go to news group main page
        {
            processRequest(request, response);  // rename the method
        }
        
        else
        {
            handlePopupMessage(request,response);  // popup a window for reply
            
        }//*/
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Servlet that handles course news group requests";
    }
    // </editor-fold>
}

/*
Arial face="Arial" 
Arial Black  face="Arial Black" 
Comic Sans MS  face="Comic Sans MS" 
Courier New face="Courier New" 
Georgia  face="Georgia" 
Impact face="Impact"  
Times New Roman  face="Times New Roman" 
Trebuchet MS face="Trebuchet MS" 
Verdana  face="Verdana" 
 
 * 
 */
